package com.chrray.position.domain;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.chrray.position.auth.Authentication;
import com.chrray.position.auth.AuthenticationContext;
import com.chrray.position.enums.YesNoEnum;
import com.chrray.position.enums.app.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.*;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.util.Assert;

import javax.persistence.Transient;
import java.util.Date;

/**
 * 业务系统用户 对象
 *
 * @author wusiwee
 * @email wusiwee@163.com
 * @since 1.0.1 2024-08-07
 */

@Data
@ApiModel(description = "业务系统用户")
@TableName("fd_user")
public class FdUser extends Model<FdUser> {
	@ApiModelProperty(value = "主键")
	@TableId(type = IdType.AUTO)
	private Long id;

	@ApiModelProperty(value = "关联系统用户")
	private Long sysUserId;

	@ApiModelProperty(value = "微信open id")
	private String wxId;

	@ApiModelProperty(value = "微信头像url")
	private String wxAvatar;

	@ApiModelProperty(value = "微信昵称")
	private String wxNickname;

	@ApiModelProperty(value = "用户姓名")
	private String name;

	@ApiModelProperty(value = "手机号")
	private String phone;

	@ApiModelProperty(value = "出生年份")
	private Integer birthYear;

	@ApiModelProperty(value = "性别:1-男,2-女,3-不限")
	private Integer gender;

	@ApiModelProperty(value = "教育状态:0-在读,1-已毕业")
	private Integer eduState;

	@ApiModelProperty(value = "学历:1.初中及以下，2.高中，3.大专，4.本科，5.硕士，6博士及以上")
	private Integer eduLevel;

	@ApiModelProperty(value = "工作年限:1.无工作经验，2.  0-1年，3.  1-3年，4.  3-5年，5.  5-10年，6.  10年以上")
	private Integer workYears;

	@ApiModelProperty(value = "身高,厘米")
	private Integer height;

	@ApiModelProperty(value = "体重,公斤")
	private Integer weight;

	@ApiModelProperty(value = "自我描述")
	private String remark;

	@ApiModelProperty(value = "邮箱")
	private String mail;

	@ApiModelProperty(value = "是否实名认证: 1-已经实名,0-未实名")
	private Integer realNamed;

	@ApiModelProperty(value = "是否已个人实名认证: 1-已经实名,0-未实名")
	private Integer personalRealNamed;

	@ApiModelProperty(value = "是否已企业实名认证: 1-已经实名,0-未实名")
	private Integer entRealNamed;

	@ApiModelProperty(value = "当前登录身份: 雇工-0,雇主-1")
	private Integer currentRole;

	@ApiModelProperty(value = "当前纬度,火星坐标系")
	private Double currentLat;

	@ApiModelProperty(value = "当前经度,火星坐标系")
	private Double currentLon;

	@ApiModelProperty(value = "生活照")
	private String lifePhoto;

	@ApiModelProperty(value = "创建时间")
	private Date createTime;

	@ApiModelProperty(value = "更新时间")
	private Date updateTime;

	@ApiModelProperty(value = "用户状态,预留")
	private Integer state;

	private Integer delFlag;

    @ApiModelProperty(value = "我的发布")
    @TableField(exist = false)
    @Transient
    private Integer mySendNum;

    @ApiModelProperty(value = "我的报名")
    @TableField(exist = false)
    @Transient
    private Integer myGetNum;
    @ApiModelProperty(value = "我的收藏")
    @TableField(exist = false)
    @Transient
    private Integer myLikeNum;

    @ApiModelProperty(value = "统计事件")
    private Date staTime;


	private static String randomName() {
		String random = RandomStringUtils.random(5, true, true);
		return "游客" + random;
	}

	/**
	 * 创建微信游客用户
	 * @param openId 微信openId
	 * @return 用户对象
	 */
	public static FdUser createWxTourist(String openId) {
		String randomName = randomName();
		FdUser user = new FdUser();
		user.setWxId(openId);

		user.setWxNickname(randomName);

		user.setName(randomName);
		//user.setPhone("13000000000");
		//user.setBirthYear(1995);
		//user.setGender(FdGenderEnum.MALE);
		//user.setEduState(FdEduStateEnum.GRADUATED);
		//user.setEduLevel(FdEduLevelEnum.UNDERGRADUATE);
		//user.setWorkYears(FdWorkYearEnum.TEN_MORE);
		//user.setHeight(180);
		//user.setWeight(70);


		user.setRealNamed(YesNoEnum.NO.getCode());
		user.setPersonalRealNamed(YesNoEnum.NO.getCode());
		user.setEntRealNamed(YesNoEnum.NO.getCode());
		user.setCurrentRole(FdUserRoleEnum.WORKER.getValue());
		user.setCreateTime(new Date());
		user.setUpdateTime(new Date());
		user.setDelFlag(YesNoEnum.NO.getCode());
		user.setState(YesNoEnum.NO.getCode());
        user.setStaTime(new Date());
		user.insert();
		return user;
	}


	public FdUser findUserByWxId(String wxId) {
		LambdaQueryWrapper<FdUser> query = new LambdaQueryWrapper<FdUser>();
		query.eq(FdUser::getWxId, wxId);
		query.eq(FdUser::getDelFlag, 0);
		return  this.selectOne(query);
	}

	public static FdUser getCurrentUser() {
		Authentication context = AuthenticationContext.context();
		Assert.notNull(context, "用户未登录");
		return new FdUser().findUserByWxId(context.getWxId());
	}
}
